package com.xbongbong.crm.help;

import com.xbongbong.paas.field.FieldAttrEntity;

import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;

/**
 * 字段解释工具类
 * @author haibin.zhang
 */
public class FieldAttrHelper {

    /**
     * 创建字段列表
     * @param clazz
     * @param filterFieldList 需要过滤的表头
     * @param <T>
     */
    public static <T> void createList(Class<T> clazz, List<String> filterFieldList, Integer businessType, List<FieldAttrEntity> fieldAttrEntityList, Map<String, FieldAttrEntity> explainMap) {
        Object[] objs = clazz.getEnumConstants();
        for (Object obj : objs) {
            try {
                String attr = (String) clazz.getMethod("getSaasAttr").invoke(obj);
                if (!filterFieldList.isEmpty() && filterFieldList.contains(attr)) {
                    //丢弃需要过滤的表头字段
                    continue;
                }
                String attrName = (String) clazz.getMethod("getAttrName").invoke(obj);
                Integer fieldType = (Integer) clazz.getMethod("getFieldType").invoke(obj);
//                List<ItemPojo> itemPojoList = (List<ItemPojo>) clazz.getMethod("getItems").invoke(obj);
                FieldAttrEntity fieldAttrEntity = new FieldAttrEntity();
                fieldAttrEntity.setAttr(attr);
                fieldAttrEntity.setAttrName(attrName);
                fieldAttrEntity.setFieldType(fieldType);
//                fieldAttrEntity.setItem(itemPojoList);
//
//                if (filter(fieldAttrEntity, businessType)) {
//                    continue;
//                }

                fieldAttrEntityList.add(fieldAttrEntity);
//                FieldAttrEntity fieldAttrEntityExt = new FieldAttrEntity(fieldAttrEntity);
//                if (Objects.nonNull(itemPojoList)) {
//                    fieldAttrEntityExt.setItemMap(itemPojoList.stream().collect(Collectors.toMap(ItemPojo::getValue,ItemPojo::getText)));
//                }
                explainMap.put(attr,fieldAttrEntity);
            } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 字段过滤、处理
     * @param fieldAttrEntity
     * @param businessType
     * @return 是否需要过滤
     */
//    private static boolean filter(FieldAttrEntity fieldAttrEntity, Integer businessType) {
//        detailFilter(fieldAttrEntity,businessType);
//        return false;
//    }
//
//    private static void detailFilter(FieldAttrEntity fieldAttrEntity, Integer businessType) {
//        fieldAttrEntity.setDetailThrough(DetailThroughEnum.isThroughAttr(businessType,fieldAttrEntity.getAttr()));
//    }

}
